// 导入数据库连接对象
const db = require("../db");
// 导入配置信息
const { debug, info } = require("../config");

/**
 * 获取所有分类
 */
/* exports.getCategory = async (req, res) => {
  const { page = 1, size = 5, article_title } = req.query;
  // 计算偏移量
  const offset = (page - 1) * size;
  let sql = `select art.*,sort.name from blog_article art,blog_sort sort 
  where art.category_id=sort.id limit ${offset},${size}`;
  const sql1 = `select art.*,sort.name from blog_article art,blog_sort sort 
  where art.category_id=sort.id and art.article_title like '%${article_title}%' limit ${offset},${size}`;
  if (article_title != "") {
    var [rows] = await db.promise().query(sql1);
    sql = `select count(*) as total from (${sql1}) middle_table`;
  } else {
    var [rows] = await db.promise().query(sql);
    sql = `select count(*) as total from blog_article`;
  }
  // 计算总数
  const [result] = await db.promise().query(sql);
  if (rows.length != 0 && result.length != 0) {
    var { total } = result[0];
    // console.log(total);
    res.send(info(0, "获取所有文章成功", { total, rows }));
  } else {
    res.send(info(0, "暂无数据", []));
  }
}; */

/**
 * 获取所有分类名称
 */
exports.getCategoryName = async (req, res) => {
  let sql = `select id,name from blog_sort `;
  const [rows] = await db.promise().query(sql);
  if (rows.length != 0) {
    res.send(info(0, "获取分类名称成功", rows));
  }
};
/**
 * 删除分类
 */
/* exports.deleteArticle = async (req, res) => {
  const { ids } = req.body;
  for (let i = 0; i < ids.length; i++) {
    let sql = `delete from blog_article where id=${ids[i]}`;
    var [rows] = await db.promise().query(sql);
  }
  res.send(info(0, "删除文章成功", ""));
}; */
